/**
 * @module ChiVoxSDK
 * @author Don Li <kai.li@chivox.com>
 * @licence
 * Copyright (c) 2017 - 2020 ChiVox Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import Utils from './utils';
import cfg from './config.json';

const DIALOG_CONTENT = `
<div class="dialogTitle">设置面板<button class="dialogCloseButton">X</button></div>
<div class="dialogFlashError">
  <p class="flashVersion hidden">
  您电脑的Flash版本过低，请点击此链接升级。
  <br/>
  <a href="http://get.adobe.com/flashplayer/" target="_blank">点击下载最新Flash版本</a>
  </p>
  <p class="flashPlugin hidden">
  您的电脑没有安装或者禁用Flash插件，请确认。
  <br/>
  <a href="http://get.adobe.com/flashplayer/" target="_blank">点击下载最新Flash版本</a>
  </p>
</div>
<div class="dialogBody"></div>
<div class="dialogInfo">
  <h5>状态信息</h5>
  <ul>
    <li class="loadFlashPlayer hidden"><span class="info">正在下载Flash播放器...</span></li>
    <li class="loadFlashRecorder hidden"><span class="info">正在下载Flash录音机...</span></li>
    <li class="connectServerInit hidden"><span class="error">还没有连接录音服务器</span></li>
    <li class="connectServerStart hidden"><span class="info">正在连接录音服务器，请稍候...</span></li>
    <li class="connectServerSuccess hidden"><span class="success">连接录音服务器成功</span></li>
    <li class="micStatusAllow hidden"><span class="success">Flash可以访问麦克风</span></li>
    <li class="micStatusDisallow hidden"><span class="error">Flash不能访问麦克风，请设置</span></li>
    <li class="micStatusError hidden"><span class="error">没有找到录音设备，请检查</span></li>
  </ul>
</div>
<div class="dialogButton"><button class="okButton">确定</button></div>
`;

/**
 * Dialog类。<br/></br/>
 *
 * ChiVoxSDK初始化及使用中，需要显示播放器／录音机／网络状态／麦克风相关的状态设置对话框。 <br/>
 * 当以下状态出现时，会弹出对话框：<br/>
 * 1. 开始下载Flash播放器<br/>
 * 2. 开始连接评分服务器<br/>
 * 3. 连接评分服务器失败<br/>
 * 4. Flash插件不可用<br/>
 * 5. 麦克风或录音设备不能使用
 */
class Dialog {
    constructor() {
        this.__ele_overlay = Utils.find(document, cfg.selector.dialogOverlay);
        this.__ele_dialog  = Utils.find(document, cfg.selector.mediadialog);

        if (!(this.__ele_dialog && this.__ele_overlay)) {
            this.__insert_dialog_elements();
        }

        this.__init = true;
        this.close();
    }

    __insert_dialog_elements() {
        let ele_root = document.createElement('div');

        this.__ele_overlay = document.createElement('div');
        this.__ele_overlay.id = cfg.selector.dialogOverlay.substr(1);
        this.__ele_overlay.setAttribute('class', cfg.selector.dialogOverlay.substr(1));

        this.__ele_dialog = document.createElement('div');
        this.__ele_dialog.id = cfg.selector.mediadialog.substr(1);
        this.__ele_dialog.setAttribute('class', cfg.selector.mediadialog.substr(1));
        this.__ele_dialog.innerHTML = DIALOG_CONTENT;

        ele_root.appendChild(this.__ele_overlay);
        ele_root.appendChild(this.__ele_dialog);

        document.body.appendChild(ele_root);
    }

    /**
     * 打开通用的信息提示框。
     */
    open() {
        if (!this.__init) return;

        this.__ele_overlay.style.display = '';
        //this.__ele_dialog.style.display = '';

        Utils.removeClass(this.__ele_dialog, 'aiMediaDialogClosed');
        Utils.addClass(this.__ele_dialog, 'aiMediaDialogSuccess');
        Utils.addClass(this.__ele_dialog, 'aiMediaDialogError');
    }

    /**
     * 打开Flash相关错误信息提示框。
     */
    open_flash_error() {
        if (!this.__init) return;

        this.__ele_overlay.style.display = '';

        Utils.removeClass(this.__ele_dialog, 'aiMediaDialogClosed');
        Utils.addClass(this.__ele_dialog, 'aiMediaDialogSuccess');
        Utils.addClass(this.__ele_dialog, 'aiMediaDialogError');

        let ele_flashplugin = Utils.find(this.__ele_dialog, '.flashPlugin');
        if (ele_flashplugin) {
            Utils.removeClass(ele_flashplugin, 'hidden');
        }
    }

    /**
     * 关闭当前的信息提示框。
     */
    close() {
        if (!this.__init) return;

        this.__ele_overlay.style.display = 'none';
        //this.__ele_dialog.style.display = 'none';

        Utils.removeClass(this.__ele_dialog, 'aiMediaDialogSuccess');
        Utils.removeClass(this.__ele_dialog, 'aiMediaDialogError');
        Utils.addClass(this.__ele_dialog, 'aiMediaDialogClosed');
    }
}

export default Dialog
